package com.carambatv.android.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import android.util.Log;

public class HttpConnector {
	private static final String TAG = HttpConnector.class.getSimpleName();

	private static HttpConnector instance = null;
	private HttpClient mClient;

	private HttpConnector() {
		final HttpParams params = new BasicHttpParams();
		params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
		params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
		params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		HttpConnectionParams.setConnectionTimeout(params, 9000);
		HttpConnectionParams.setSoTimeout(params, 9000);

		final SchemeRegistry registry = new SchemeRegistry();
		registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
		final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);

		mClient = new DefaultHttpClient(manager, params);

	};

	public static HttpConnector getInstance() {
		if (instance == null)
			instance = new HttpConnector();
		return instance;
	}

	public InputStream sendGETRequest(final String url) throws URISyntaxException, ClientProtocolException, IOException {
		InputStream result = null;

		final HttpGet get = new HttpGet(url);

		try {
			result = executeRequestStream(get);
		} catch (IOException e) {
			Log.e(TAG, "Exception", e);
		}

		return result;
	}

	private InputStream executeRequestStream(HttpGet get) throws IOException {
		InputStream result = null;
		HttpEntity entity = null;
		try {
			final HttpResponse response = mClient.execute(get);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = response.getEntity().getContent();
			}
		} finally {
			if (entity != null) {
				entity.consumeContent();
			}
		}
		return result;
	}

}
